// import com.alibaba.druid.sql.ast.*;
// import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
// import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

// import java.util.ArrayList;
// import java.util.std::list;

#include "SQLShowColumnsStatement.h"
#include "../SQLName.h"
#include "../SQLExpr.h"
#include "../SQLObject.h"
#include "../../visitor/SQLASTVisitor.h"
#include "../../../utils/instanceof.h"
#include "../expr/SQLIdentifierExpr.h"
#include "../expr/SQLPropertyExpr.h"

BOOL_ptr SQLShowColumnsStatement::isFull()
{
  return full;
}

void SQLShowColumnsStatement::setFull(BOOL_ptr full)
{
  this->full = full;
}

SQLName_ptr SQLShowColumnsStatement::getTable()
{
  return table;
}

void SQLShowColumnsStatement::setTable(SQLName_ptr table)
{
  if (instanceof <SQLName, SQLPropertyExpr>(table))
  {
    SQLPropertyExpr_ptr propExpr = std::dynamic_pointer_cast<SQLPropertyExpr>(table);
    this->setDatabase(std::dynamic_pointer_cast<SQLName>(propExpr->getOwner()));
    this->table = SQLIdentifierExpr_ptr(new SQLIdentifierExpr(propExpr->getName()));
    return;
  }
  this->table = table;
}

SQLName_ptr SQLShowColumnsStatement::getDatabase()
{
  return database;
}

void SQLShowColumnsStatement::setDatabase(SQLName_ptr database)
{
  this->database = database;
}

SQLExpr_ptr SQLShowColumnsStatement::getLike()
{
  return like;
}

void SQLShowColumnsStatement::setLike(SQLExpr_ptr like)
{
  this->like = like;
}

SQLExpr_ptr SQLShowColumnsStatement::getWhere()
{
  return where;
}

void SQLShowColumnsStatement::setWhere(SQLExpr_ptr where)
{
  this->where = where;
}

void SQLShowColumnsStatement::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLShowColumnsStatement)))
  {
    acceptChild(visitor, table);
    acceptChild(visitor, database);
    acceptChild(visitor, like);
    acceptChild(visitor, where);
  }
  visitor->endVisit(SharedObject(SQLShowColumnsStatement));
}

SQLObject_list_ptr  SQLShowColumnsStatement::getChildren()
{
  SQLObject_list_ptr  children = SQLObject_list_ptr ();
  if (table != nullptr)
  {
    children->push_back(table);
  }
  if (database != nullptr)
  {
    children->push_back(database);
  }
  if (like != nullptr)
  {
    children->push_back(like);
  }
  if (where != nullptr)
  {
    children->push_back(where);
  }
  return children;
}

// @Override
BOOL_ptr SQLShowColumnsStatement::replace(SQLExpr_ptr expr, SQLExpr_ptr target)
{
  if (table == expr)
  {
    setTable(std::dynamic_pointer_cast<SQLName>(target));
    return BOOL::TRUE;
  }

  if (database == expr)
  {
    setDatabase(std::dynamic_pointer_cast<SQLName>(target));
    return BOOL::TRUE;
  }

  if (like == expr)
  {
    setLike(target);
    return BOOL::TRUE;
  }

  if (where == expr)
  {
    setWhere(target);
    return BOOL::TRUE;
  }

  return BOOL::FALSE;
}
